layui.config({
    base: 'plugins/layui/modules/'
}).use(['form', 'http'], function () {
    var  form = layui.form,
        http = layui.http;

            
    // ------------------- 商品附件 start ----------------------
    $('#fileUpLoad').on('change', function () {
        var newFlieArr = [];
        var formData = new FormData();			        
        var files = this.files;
        if(!files||files.length==0){
            return
        }
        var ifGo = true;
        for(var i=0;i<files.length;i++){
            var newFlie = {};
            var file = files[i];
            newFlie.filename = file.name;        //文件名
            fileext = http.getLaName(file.name);
            if(!http.isAssetType(fileext)){ // 文件类型判断
                ifGo = false
                layer.msg('只可上传PDF或图片')
                break
            }
            if(!http.verificationPicFile(files[i],20)){ // 大小判断
                ifGo = false
                break
            }
            newFlieArr.push(newFlie);
            formData.append("files", files[i]);
        }
        formData.append("isfileName", false);
        formData.append("fileType", "file");
        if(ifGo){
            http.upLoad('system/file/upload/batch',formData,function(res){
                $('#fileUpLoad').val('')
                if(res.code == 200){
                    var dataList = res.data;
                    for(var i=0;i<dataList.length;i++){
                        newFlieArr[i].file = dataList[i];
                        var html = '<li><span data-url="'+newFlieArr[i].file+'" class="fileUpLoadName">'+newFlieArr[i].filename+'</span><span class="fileUpLoadView">删除</span></li>';
                        $('.fileList').append(html);
                    }
                }else{
                    layer.msg(res.msg)
                }
            })  
        }else{
        }
    })
    // 预览
    $(document).on('click','.fileUpLoadName',function(e){
        var url = e.target.dataset.url
        window.open(TUrl+url)
    })
    // 删除
    $(document).on('click','.fileUpLoadView',function(e){
        $(this).parent().remove()
    })
    // 商品附件
    /* $(document).on('change','#fileUpLoad', function(e){
        var files = $('#fileUpLoad').val()
        var filename = files.replace(/.*(\/|\\)/, '');
        var fileext = (/[.]/.exec(filename)) ? /[^.]+$/.exec(filename.toLowerCase()) : '';
        //检查文件格式
        if(fileext == 'pdf'){
            // console.log(e.target)
            var file = e.target.files
            var formData = new FormData();
            formData.append('file', file[0]);
            formData.append('fileType', 'file');
            formData.append('isFileName', false);
            http.upLoad('system/file/upload',formData,function(res){
                if(res.code == 200){
                    var a = {
                        filename: filename,
                        file: res.data
                    }
                    annexFiles.push(a)
                    var index = annexFiles.length
                    $('.fileUpLoadDiv').show()
                    var html = '<div><span class="fileUpLoadName" data-file="'+res.data+'">'+filename+'</span><span class="fileUpLoadView" data-index="'+index+'">删除</span></div>'
                    $('.fileUpLoadDiv').append(html)

                }else{
                    layer.msg(res.msg)
                }
            })
            $('#fileUpLoad').replaceWith('<input type="file" accept="application/pdf" id="fileUpLoad" multiple style="display: none;" name="">');
        }else{
            layer.msg('只可上传PDF')
        }
    }) 
    // 预览
    $(document).on('click','.fileUpLoadName',function(e){
        var file = e.target.dataset.file
        window.open(TUrl+file)
    })
    // 删除
    $(document).on('click','.fileUpLoadView',function(e){
        var index = e.target.dataset.index
        index = Number(index)
        annexFiles.splice(index,1)
        $('.fileUpLoadDiv>div').eq(index).remove()
    }) */
    // ------------------- 商品附件 end ----------------------
			
})